﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式之禅.观察者模式
{
    /// <summary>
    /// 抽象被观察者
    /// </summary>
    public abstract class AbstractSubject
    {
        private List<AbstractObservable> observables = new List<AbstractObservable>();
        public void AddObservable(AbstractObservable observable)
        {
            this.observables.Add(observable);
        }
        public void RemoveObservable(AbstractObservable observable)
        {
            this.observables.Remove(observable);
        }
        public void Notify()
        {
            observables.ForEach(o => o.update());
        }
        public abstract string Say();
    }

    /// <summary>
    /// 具体被观察者
    /// </summary>
    public class Subject : AbstractSubject
    {
        public override string Say()
        {
            this.Notify();
            return "A";
        }
    }

    /// <summary>
    /// 抽象观察者
    /// </summary>
    public abstract class AbstractObservable
    {
        public abstract void update();
    }

    /// <summary>
    /// 具体观察者
    /// </summary>
    public class ObservableA : AbstractObservable
    {
        public override void update()
        {
            //DO
        }
    }

    public class ObservableB : AbstractObservable
    {
        public override void update()
        {
            //DO
        }
    }

}
